angular.module('videoservice', [])
  .factory('Video', function($resource,$rootScope,ENV) {


    var APIUrl = ENV.api;
    var videoListJson,pageSize=6,collectInfoJson,
      videoDetailJson,collectVideoJson,likeVideoJson,hasVideoJson;
    var videoListResource = $resource(APIUrl+"bookshelf/getVideoList.do");
    var videoDetailResource = $resource(APIUrl+"video/getVideoDetail.do");
    var collectVideoResource = $resource(APIUrl+"video/collectVideo.do");
    var likeVideoResource = $resource(APIUrl+"video/likeVideo.do");
    var getCollectInfoResource = $resource(APIUrl+"bookshelf/getCollectInfo.do");
    var hasVideoResource = $resource(APIUrl+"video/hasVideo.do");
    return {
      //根据视频编号获取是否存在该视频
      getVideoById:function(videoId){
        hasVideoResource.save({
          video_id:videoId
        },function(response){
          hasVideoJson = response.hasVideo;
          $rootScope.$broadcast('Video.hasVideoUpdated');
        });
      },
      hasVideoOrNot:function(){
        return hasVideoJson;
      },
      //获取书架的收藏信息
      getCollectInfo:function(type,searchList){
        getCollectInfoResource.save({
            type:type,
            search_list:searchList
        },function(response){
          collectInfoJson = response;
          $rootScope.$broadcast('BookShelf.getCollectInfoUpdated');
        });
        },
      getCollectInfoJson:function(){
        return collectInfoJson;
      },
      //获取到视频列表
      getVideoList:function(memberId,size){
        var _pageSize = size|| pageSize;
        var hasNextPage = true;
        videoListResource.save({
          member_id:memberId,
          pageIndex:1,
          pageSize:_pageSize
        },function(response){
          if(response.videoList.length<_pageSize){
            hasNextPage = false;
          }
          videoListJson = {
            hasNextPage:hasNextPage,
            nextPage:2,
            data:response.videoList
          };
          $rootScope.$broadcast('Video.videoListUpdated');
        });
      },
      //加载更多视频
      getMoreVideoList:function(memberId){
        if(videoListJson===undefined){
          return false;
        }
        var hasNextPage=videoListJson.hasNextPage;
        var nextPage=videoListJson.nextPage;
        var moreCommentData=videoListJson.data;

        if(!hasNextPage){
          return false;
        }
        videoListResource.save({
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.videoList.length<pageSize){
            hasNextPage = false;
          }
          moreCommentData = moreCommentData.concat(response.videoList);
          videoListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreCommentData
          };
          $rootScope.$broadcast('Video.MoreVideoListUpdated');
        });
      },
      videoHasNextPage:function(){
        if(videoListJson===undefined){
          return false;
        }
        return videoListJson.hasNextPage;
      },
      //获取视频详情
      getVideoDetail:function(memberId,videoId){
        videoDetailResource.save({
          member_id:memberId,
          video_id:videoId
        },function(response){
          videoDetailJson = response;
          $rootScope.$broadcast('Video.videoDetailUpdated');
        })
      },
      //返回视频列表的Json
      getVideoListJson:function(){
        if(videoListJson===undefined){
          return false;
        }
        return videoListJson.data;
      },
      //返回视频详情Json
      getVideoDetailJson:function(){
        return videoDetailJson;
      },
      //收藏该视频
      collectVideo:function(memberId,videoId){
        collectVideoResource.save({
          member_id:memberId,
          mc_obj_id:videoId
        },function(response){
          collectVideoJson = response;
          $rootScope.$broadcast('Video.collectVideoUpdated');
        });

      },
      //点赞该视频
      likeVideo:function(memberId,videoId){
        likeVideoResource.save({
          member_id:memberId,
          video_id:videoId
        },function(response){
          likeVideoJson = response;
          $rootScope.$broadcast('Video.likeVideoUpdated');
        });
      },
      //返回收藏的信息
      getCollectJson:function(){
        return collectVideoJson;
      },
      //返回点赞的信息
      getLikeJson:function(){
        return likeVideoJson;
      }
    }
  })

//评论的Service
  .factory('VideoComment', function($resource,$rootScope,ENV) {
    var APIUrl = ENV.api;

    var commentListJson,pageSize=10,
      memberInfo,videoCommentJson,commentCount,videoTitle;

    var videoCommentResource = $resource(APIUrl+"video/getVideoCommentList.do");
    var publishCommentResource = $resource(APIUrl+"video/commentVideo.do");
    var commentCountResource = $resource(APIUrl+"video/getCommentCount.do");
    var deleteCommentResource = $resource(APIUrl+"video/delVideoComment.do");


    //获取的新闻的评论列表
    return {
      //删除评论
      deleteComment:function(commentId,memberId,videoId){
        deleteCommentResource.save({
          video_comment_id:commentId,
          member_id:memberId,
          video_id:videoId
        },function(response){
          deleteCommentJson = response;
          $rootScope.$broadcast('VideoComment.deleteCommentUpdated');
        });
      },
      getDelCommentJson:function(){
        return deleteCommentJson;
      },
      /**********************************************/
      getCommentList:function(videoId,memberId){
        var hasNextPage = true;
        videoCommentResource.save({
          video_id:videoId,
          member_id:memberId,
          pageIndex:1,
          pageSize:pageSize
        },function(response){
          if(response.commentList.length<pageSize){
            hasNextPage = false;
          }
          memberInfo = response.memberInfo;
          videoTitle = response.videoTitle;
          commentListJson = {
            hasNextPage:hasNextPage,
            nextPage: 2,
            data: response.commentList
          };
          $rootScope.$broadcast('Video.CommentUpdated');
        })
      },
      getMoreCommentList:function(videoId,memberId){
        if(commentListJson===undefined){
          return false;
        }
        var hasNextPage=commentListJson.hasNextPage;
        var nextPage=commentListJson.nextPage;
        var moreCommentData=commentListJson.data;
        if(!hasNextPage){
          return false;
        }
        videoCommentResource.save({
          video_id:videoId,
          member_id:memberId,
          pageIndex:nextPage,
          pageSize:pageSize
        },function(response){
          nextPage++;
          if(response.commentList.length<pageSize){
            hasNextPage = false;
          }
          moreCommentData = moreCommentData.concat(response.commentList);
          commentListJson = {
            hasNextPage:hasNextPage,
            nextPage: nextPage,
            data: moreCommentData
          };
          $rootScope.$broadcast('Video.MoreCommentListUpdated');
        });
      },
      //是否有下一页
      hasNextPage: function() {
        if (commentListJson === undefined) {
          return false;
        }
        return commentListJson.hasNextPage;
      },
      //获取到视频详情的标题
      getVideoTitleJson:function(){
        return videoTitle;
      },
      getMemberInfo:function(){
        return memberInfo;
      },
      /***************************************/
     /* getCommentList:function(videoId,memberId){
        videoCommentResource.save({
          video_id:videoId,
          member_id:memberId
        },function(response){
          commentListJson = response;
          $rootScope.$broadcast('Video.CommentUpdated');
        })
      },*/
      getCommentCount:function(videoId,memberId){
        commentCountResource.save({
          video_id:videoId,
          member_id:memberId
        },function(reponse){
          commentCount = reponse;
          $rootScope.$broadcast('Video.CommentCountUpdated');
        })
      },
      //发表评论
      publishComment:function(memberId,videoId,newComment){
        publishCommentResource.save({
          member_id:memberId,
          video_id:videoId,
          video_comment_content:newComment
        },function(response){
          videoCommentJson = response;
          $rootScope.$broadcast('Video.publishCommentUpdated');
        });
      },
      getCommentJson:function(){
        return commentListJson.data;
      },
      //获取到新增的评论内容
      getNewComment:function(){
        return videoCommentJson;
      },
      //获取评论的数量
      getCommetCountJson:function(){
        return commentCount;
      }


    }

  })
  ;



